const app = getApp()

Page({
  data: {
    account: "",
    password: "",
    state: '',
  },




  //判断是不是正版
  bdloging() {

  },

  //登陆/ guibi为1时需要密码
  loging(account, password) {
    console.log("有密码的");
    wx.request({
      url: 'https://churuku.app12345.cn/api/Login/xcx_login',
      method: 'POST',
      data: {
        account: account,
        pwd: password
      },
      success: (res) => {
        wx.showToast({
          title: '登陆成功',
          icon: 'succse',
          duration: 1500
        })
        console.log(res.data.data);
        let user = res.data.data
        wx.setStorageSync('user_id', user.id)
        wx.setStorageSync('token', user.token)
        setTimeout(() => {
          this.router()
        }, 1500)
      }
    })
  },
  //跳转
  router() {
    wx.switchTab({
      url: '../select/select',
    })
  },

  //登陆 
  logings(account, password) {
    console.log("没密码的");
    wx.request({
      url: `${app.globalData.api}/api/Login/xcx_login`,
      method: 'POST',
      data: {
        account: account,
        pwd: password
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.showToast({
            title: '登陆成功',
            icon: 'succse',
            duration: 1500
          })
          console.log(res.data.data);
          let user = res.data.data
          wx.setStorageSync('user_id', user.id)
          wx.setStorageSync('token', user.token)
          setTimeout(() => {
            this.router()
          }, 2000)
        } else {
          wx.showToast({
            title: '账号或密码错误',
            icon: 'error',
            duration: 1500
          })
        }
      }
    })
  },
  onShow() {
    this.positive_ornegative()
  },
  //是否为正式版
  positive_ornegative() {
    wx.request({
      url: 'https://churuku.app12345.cn/api/Login/xcx_guibi',
      method: 'POST',
      success: (res) => {
        console.log(res.data.data.guibi);
        this.setData({
          state: res.data.data.guibi
        })
        console.log(this.data.state);
        if (this.data.state == 1) {
          this.setData({
            account: "admin",
            password: "123456",
          })
          this.loging(this.data.account, this.data.password)
        }
      }

    })
  },
  formSubmit(e) {
    console.log(this.data.state);
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    var user = e.detail.value

    if (this.data.state == 0) { //0账号要密码
      if (user.account == '' || user.password == '') {
        wx.showToast({
          title: '账号密码不能为空',
          icon: 'none',
          duration: 1500
        })
      } else {
        this.logings(user.account, user.password); //无须密码
      }
    }
  },

})